package com.hoki.zj.hrm.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.hoki.zj.hrm.constant.MQMessageConst.*;

@Configuration
public class MQConfig {

    @Bean
    public Exchange exchange(){
        return ExchangeBuilder.topicExchange(EXCHANGE_NAME_PAY_RESULT).build();
    }


    @Bean
    public Queue queue(){
        return new Queue(QUEUE_NAME_PAY_RESULT);
    }

    //绑定队列
    @Bean
    public Binding bindingDelayQueue(){
        return BindingBuilder.bind(queue()).to(exchange())
                .with("#.pay").noargs();
    }

    //自定义RabbitTemplate
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        //如果消息发送到MQ失败，把消息返回回来
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }
}
